//
//  XZTrainController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZTrainController.h"
#import "XZTrainNumController.h"
#import "XZSTSController.h"
#import "XZLeftTicketController.h"

@interface XZTrainController ()
//@property (weak, nonatomic) UISegmentedControl *segementControl;
@property (strong, nonatomic) XZTrainNumController *trainNumController;
@property (strong, nonatomic) XZLeftTicketController *leftTicketController;
@property (strong, nonatomic) XZSTSController *stationToStationController;
//@property (strong, nonatomic) NSMutableDictionary *controllerDict;
@property (strong, nonatomic) UIViewController *selectedController;
@end

@implementation XZTrainController

- (NSMutableDictionary *)controllerDict
{
    if (!_controllerDict) {
        _controllerDict = [NSMutableDictionary dictionary];
    }
    return _controllerDict;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"火车查询";
    
    self.view.backgroundColor = kGlobalBackgroundColor;
    
    [self addViews];

}

- (void)addViews
{
    UISegmentedControl *segementControl = [[UISegmentedControl alloc] initWithItems:@[@"车次", @"站到站", @"余票"]];
    segementControl.y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    segementControl.width = self.view.width;
    [segementControl addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    self.segementControl = segementControl;
    [self.view addSubview:segementControl];
    
    segementControl.selectedSegmentIndex = 0;

    self.trainNumController = [[XZTrainNumController alloc] init];
    self.leftTicketController = [[XZLeftTicketController alloc] init];
    self.stationToStationController = [[XZSTSController alloc] init];
    [self addChildViewController:self.trainNumController];
    [self addChildViewController:self.leftTicketController];
    [self addChildViewController:self.stationToStationController];
    
    [self.view addSubview:self.trainNumController.view];
    self.selectedController = self.trainNumController;
    
    self.controllerDict[@(XZTrainSubControllerTypeTrainNum)] = self.trainNumController;
    self.controllerDict[@(XZTrainSubControllerTypeStationToStation)] = self.stationToStationController;
    self.controllerDict[@(XZTrainSubControllerTypeLeftTicket)] = self.leftTicketController;
}

- (void)changeValue:(UISegmentedControl *)segementControl
{
    // 设置新选中的controller
    UIViewController *controller = self.controllerDict[@(segementControl.selectedSegmentIndex)];
    // 将新选中的Controller中的View添加到当前View
    [self.view addSubview:controller.view];
    // 移除旧View
    [self.selectedController.view removeFromSuperview];
    // 重置选中的Controller
    self.selectedController = controller;
}

@end
